﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;

namespace YY.UI
{
    /// <summary>
    /// 自定义导航菜单控件 by DWX
    /// </summary>
    [ToolboxItem(true)]
    public partial class NavMenuBar : UserControl
    {
        /// <summary>
        /// 公开的导航菜单单击事件
        /// </summary>
        public event EventHandler OnMenuSelection;
        /// <summary>
        /// 公开菜单树节点选中事件
        /// </summary>
        public event TreeNodeMouseClickEventHandler OnTreeNodeMouseClick;
        private static ArrayList NavItems = new ArrayList();
        private static string selectedItem = string.Empty;

        private System.Resources.ResourceManager rm = new System.Resources.ResourceManager("Resources", System.Reflection.Assembly.GetExecutingAssembly());

        public NavMenuBar()
        {
            InitializeComponent();
        }

        public ArrayList MenuItems
        {
            set
            {
                NavItems = value;
            }
            get
            {
                return (NavItems);
            }
        }

        /// <summary>
        /// 加载展示菜单
        /// </summary>
        public void RenderMenu()
        {
            if (NavItems.Count == 0) return;
            LoadMenuItems(NavItems);
        }

        private void LoadMenuItems(ArrayList NavItems)
        {
            int btnHeight = 30;// Properties.Resources.btnBgnd.Height;//菜单项高度默认展示背景图高度

            Panel mainPanel = new Panel();//主Panel
            mainPanel.Dock = DockStyle.Top;
            mainPanel.AutoSize = true;
            mainPanel.Name = "mainPanel";

            Panel pnl = new Panel();//副Panel
            pnl.Height = 20;
            pnl.Dock = DockStyle.Top;
            mainPanel.Controls.Add(pnl);
            string selectedButton = string.Empty;
            foreach (NavItem navItem in NavItems)
            {
                Button btn = new Button();
                btn.Height = btnHeight;
                //btn.BackgroundImage = (Image)Properties.Resources.btnBgnd;
                btn.Dock = DockStyle.Bottom;
                btn.Name = navItem.MnuKey;
                btn.Click += new EventHandler(btn_Click);
                btn.Text = navItem.MnuText;

                if (navItem.Selected == true) selectedButton = btn.Name;

                mainPanel.Controls.Add(btn);


                pnl = new Panel();
                pnl.Dock = DockStyle.Bottom;
                pnl.Name = "panel_" + navItem.MnuKey;
                pnl.Visible = false;
                if (navItem.MnuChildTreeView != null)
                {
                    pnl.Height = navItem.MnuChildTreeView.Height;
                    navItem.MnuChildTreeView.Dock = DockStyle.Fill;
                    navItem.MnuChildTreeView.ShowPlusMinus = true;
                    navItem.MnuChildTreeView.ExpandAll();
                    navItem.MnuChildTreeView.HotTracking = true;
                    navItem.MnuChildTreeView.HideSelection = false;
                    navItem.MnuChildTreeView.Name = "treeview_" + navItem.MnuKey;
                    navItem.MnuChildTreeView.NodeMouseClick += new TreeNodeMouseClickEventHandler(treeNodeMouseClick);                    
                    pnl.Controls.Add(navItem.MnuChildTreeView);
                }

                mainPanel.Controls.Add(pnl);


            }
            panel1.Controls.Add(mainPanel);


            if (selectedButton != string.Empty)
            {
                Button btn = (Button)panel1.Controls["mainPanel"].Controls[selectedButton];
                selectedItem = btn.Name;
                ShowChilds(btn);
            }
        }

        private void btn_Click(object sender, EventArgs e)
        {
            if (selectedItem != ((Button)sender).Name)
            {
                ShowChilds((Button)sender);
            }


            //OnMenuSelection(sender, e);
            selectedItem = ((Button)sender).Name;

        }

        private void ShowChilds(Button btn)
        {
            ResetAllPanels();

            Panel pnl = (Panel)panel1.Controls["mainPanel"].Controls["panel_" + btn.Name];
            pnl.Visible = true;

        }

        /// <summary>
        ///树节点选中触发
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void treeNodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
        { OnTreeNodeMouseClick(sender, e); }

        private void ResetAllPanels()
        {
            foreach (Control ctrl in panel1.Controls["mainPanel"].Controls)
            {
                if (ctrl is Panel)
                {
                    if (ctrl.Name.Length >= 6)
                    {
                        if (ctrl.Name.Substring(0, 6) == "panel_")
                        {
                            if (ctrl.Visible == true) ctrl.Visible = false;
                        }
                    }
                }
            }
        }

        /// <summary>
        /// 导航菜单折叠项
        /// </summary>
        public class NavItem
        {
            /// <summary>
            /// 菜单名称
            /// </summary>
            public string MnuText;
            /// <summary>
            /// 菜单关键字
            /// </summary>
            public string MnuKey;
            //public ArrayList childNavItems;
            /// <summary>
            /// 菜单是否选中
            /// </summary>
            public bool Selected = false;
            /// <summary>
            /// 包含的TreeView子菜单
            /// </summary>
            public TreeView MnuChildTreeView { get; set; }

            public NavItem(string _MnuText, string _MnuKey, TreeView _MnuChildTreeView, bool _Selected)
            {
                MnuText = _MnuText;
                MnuKey = _MnuKey;
                Selected = _Selected;
                MnuChildTreeView = _MnuChildTreeView;
            }

            public NavItem(string _MnuText, string _MnuKey, TreeView _MnuChildTreeView)
            {
                MnuText = _MnuText;
                MnuKey = _MnuKey;
                MnuChildTreeView = _MnuChildTreeView;
            }
        }

        public class childNavItems
        {
            public string MnuItem;
            public string eventCode;
            public childNavItems(string _MnuItem, string _eventCode)
            {
                MnuItem = _MnuItem;
                eventCode = _eventCode;
            }
        }

        private void NavMenuBar_Load(object sender, EventArgs e)
        {

        }
    }
}
